﻿using System;
using DotNetOpenAuth.OpenId.Extensions.ProviderAuthenticationPolicy;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;
using Entity;
using HighLowTriple7;
using PaymentBLL;

public partial class userControls_ucHeader : System.Web.UI.UserControl
{
    public uspAccountInfoSelectResult Info = new uspAccountInfoSelectResult();
    protected void Page_Load(object sender, EventArgs e)
    {

        var ctr = new CtrAccount();
        
        if (!Utils.UserUtils.IsLogin)
        {
            if (State.ProfileFields != null)
            {
                //friendlyUsername.Text = Convert.ToString(State.ProfileFields.Email);
                
                var info = ctr.SelectAccountInfoByName(State.ProfileFields.Email);
                if (info == null)
                {

                    int? id = 0;
                    int type = State.LoginType ?? 0;
                    if (type == 0)
                        return;
                    string fullName = State.ProfileFields.FullName ?? State.ProfileFields.Email;
                    if (ctr.InsertAccount(State.ProfileFields.Email, State.ProfileFields.Nickname ?? "", type, fullName
                                          , State.ProfileFields.Email, 1, ref id) <= 0)
                        return;
                    if (id > 0)
                    {
                        ctr.AddAuAgAccount(id??0, 1, 10000, 5000, "Thêm tiền để test");
                        AccountSession.Login(id.Value, State.ProfileFields.Email, fullName, type);
                        Info = ctr.SelectAccountInfo(Utils.UserUtils.AccountId);
                    }
                    else
                    {
                        State.Clear();
                        AccountSession.Logout();
                    }
                }
                else
                {
                    if (info.Status > 0)
                    {
                        AccountSession.Login(info.Id, info.Name, info.FullName, info.TypeId);
                        Info = ctr.SelectAccountInfo(Utils.UserUtils.AccountId);
                    }
                    else
                    {
                        State.Clear();
                        AccountSession.Logout();
                    }
                }

            }
        }
        else
        {
            Info = ctr.SelectAccountInfo(Utils.UserUtils.AccountId);
        }
    }
    protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
    {
        State.Clear();
        AccountSession.Logout();
    }
    protected void OpenIdLogin1_LoggedInY(object sender, OpenIdEventArgs e)
    {
        try
        {
            State.LoginType = 4;
            State.FriendlyLoginName = e.Response.FriendlyIdentifierForDisplay;
            State.ProfileFields = e.Response.GetExtension<ClaimsResponse>();
            State.PapePolicies = e.Response.GetExtension<PolicyResponse>();
        }catch
        {
            Response.Write("Không thể kết nối tới yahoo");
        }
    }

    protected void OpenIdLogin1_LoggedInG(object sender, OpenIdEventArgs e)
    {
        try
        {
            State.LoginType = 3;
            State.FriendlyLoginName = e.Response.FriendlyIdentifierForDisplay;
            State.ProfileFields = e.Response.GetExtension<ClaimsResponse>();
            State.PapePolicies = e.Response.GetExtension<PolicyResponse>();
        }
        catch
        {
            Response.Write("Không thể kết nối tới gmail");
        }

        //this.devMsg.Text = "no response";

        //if (e.Response != null)
        //{
        //    friendlyUsername.Text = "response";

        //    switch (e.Response.Status)
        //    {
        //        case AuthenticationStatus.Authenticated:
        //            this.friendlyUsername.Text = "authenicated";
        //            break;
        //        case AuthenticationStatus.Canceled:
        //            this.friendlyUsername.Text = "canceled";
        //            break;
        //        case AuthenticationStatus.Failed:
        //            this.friendlyUsername.Text = "failed";
        //            break;
        //    }
        //}
    }
}